package com.example.aidldemo

import android.content.Context
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch


class MainActivity : AppCompatActivity() {
    private lateinit var refreshLayout: PullToRefreshLayout
    private lateinit var recyclerView: RecyclerView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        refreshLayout = findViewById(R.id.mRefresh)
        recyclerView = findViewById(R.id.mList)

        refreshLayout.setOnRefreshListener {
            Toast.makeText(this, "刷新或跳转页面", Toast.LENGTH_SHORT).show()
        }
        val layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        recyclerView.layoutManager = layoutManager

        val dataList = ArrayList<String>()
        for (c in 'A'..'D') {
            dataList.add(c.toString())
        }
        recyclerView.addItemDecoration(HItemDecoration())
        PagerSnapHelper().attachToRecyclerView(recyclerView)
        recyclerView.adapter = RvAdapter(dataList)
        MainScope().launch {

        }
        lifecycleScope.launch {  }
    }

    class RvAdapter(private var dataList: List<String>) : RecyclerView.Adapter<RvAdapter.RvViewHolder>() {
        override fun onCreateViewHolder(p0: ViewGroup, p1: Int): RvViewHolder {
            val view: View = LayoutInflater.from(p0.context).inflate(R.layout.item_pulltorefresh, p0, false).apply {
                layoutParams=ViewGroup.LayoutParams(getScreenWidth(p0.context)-96,ViewGroup.LayoutParams.MATCH_PARENT)
            }
            return RvViewHolder(view)
        }

        override fun getItemCount(): Int {
            return dataList.size
        }

        override fun onBindViewHolder(p0: RvViewHolder, p1: Int) {
            p0.name.text = dataList[p1]
        }


        class RvViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            var name: TextView = itemView.findViewById(R.id.tv_title)
            var image: ImageView = itemView.findViewById(R.id.iv_img)
        }

        fun getScreenWidth(context: Context): Int {
            val windowManager = context.getSystemService(WINDOW_SERVICE) as WindowManager
            val metrics = DisplayMetrics()
            windowManager.defaultDisplay.getMetrics(metrics)
            return metrics.widthPixels
        }
    }


}